#include "bencode.h"

bDictionary::bDictionary(const char *bDictionary) : bItem(B_DICTIONARY)
{
	const char *ptr = &(bDictionary[1]);
	while (ptr[0] != 'e')
	{
		bString key(ptr);
		ptr += key.GetCharOffset();

		bItem *item = bDecode(ptr);
		ptr += item->GetCharOffset();

		m_dictionary.insert(pair<string, bItem*>(key.GetString(), item));
	}
	m_charoffset = ptr+1 - bDictionary;
}

bDictionary::~bDictionary()
{
	map<string, bItem*>::iterator i;
	for (i = m_dictionary.begin(); i != m_dictionary.end(); i++)
	{
	    delete (*i).second;
	}
	m_dictionary.clear();
}

bItem* bDictionary::GetItemFromKey(const string key)
{
	return m_dictionary[key];
}
